package com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageMaterielBatchDetailActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySupplierListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/22 15:05
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageSelectReceiveMaterialAdapter extends BaseAdapter {

    private Activity mContext;
    private List<InventorySupplierListBean.SupplierListBean> list = new ArrayList<InventorySupplierListBean.SupplierListBean>();
    private LayoutInflater inflater;
    private String code;
    private EditText codeEditText;

    public CYChangeStorageSelectReceiveMaterialAdapter(Activity context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        InventorySupplierListBean.SupplierListBean bean = new InventorySupplierListBean.SupplierListBean();
        bean.setSupplier_code("41000000209");
        bean.setSupplier_name("江苏柏林打通电子有限公司16120816");
        for (int i = 0; i < 4; i++) {
            list.add(bean);
        }
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_change_storage_list_first_item, null);
            codeEditText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            codeEditText.setHint(R.string.activity_inventory_select_speciallnv_code_please_input_supplier_code);
            if(!TextUtils.isEmpty(code)){
                codeEditText.setText(code);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    CYChangeStorageSelectReceiveMaterialAdapter.this.code = codeEditText.getText().toString();
                    getData(mContext, CYChangeStorageSelectReceiveMaterialAdapter.this.code);
                }
            });
        } else {
            InventorySupplierListBean.SupplierListBean materielItem = list.get(position - 1);
            CYChangeStorageSelectReceiveMaterialAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_change_storage_search_receive_material, null);
                viewHolder = new CYChangeStorageSelectReceiveMaterialAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (CYChangeStorageSelectReceiveMaterialAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(CYChangeStorageSelectReceiveMaterialAdapter.ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.materialCode);
        viewHolder.description = (TextView) view.findViewById(R.id.materialDescription);
    }

    private void initData(Context context, CYChangeStorageSelectReceiveMaterialAdapter.ViewHolder viewHolder, View view, InventorySupplierListBean.SupplierListBean item) {
        viewHolder.code.setText(item.getSupplier_code());
        viewHolder.description.setText(item.getSupplier_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(ChangeStorageExtra.MATERIAL_CODE, viewHolder.code.getText().toString());
                intent.putExtra(ChangeStorageExtra.MATERIAL_ID,  viewHolder.description.getText().toString());
                mContext.setResult(ChangeStorageExtra.SELECT_MATERIAL_CODE, intent);
                mContext.finish();
            }
        });
    }

    private class ViewHolder {
        private TextView code, description;
    }

    public void search(String condition){
        this.code = condition;
        getData(mContext, condition);
    }

    private void getData(Activity activity, String condition) {
        InventoryManageRequest.findSupplier(activity, condition, new GsonBeanListener<InventorySupplierListBean>() {
            @Override
            public void onSuccess(InventorySupplierListBean bean) {
                if(bean.getBody()!=null){
                    CYChangeStorageSelectReceiveMaterialAdapter.this.list = bean.getBody();
                    CYChangeStorageSelectReceiveMaterialAdapter.this.notifyDataSetChanged();
                }

            }
        });
    }
}
